﻿using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using WPFExcelReport.Helpers;
using System.Data;
namespace WPFExcelReport
{
    public partial class SJHVK05
    {
        VICC_ABPOS_MCO MCO = new VICC_ABPOS_MCO();
        VICC_ABAUFM VMUI = new VICC_ABAUFM();
        #region ==ABAUF===========
        public static readonly DependencyProperty ABAUFProperty = DependencyProperty.Register("ABAUF",
            typeof(string), typeof(SJHVK05),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnABAUFChanged));
        public string ABAUF
        {
            get
            {
                return (string)GetValue(ABAUFProperty);
            }
            set
            {
                SetValue(ABAUFProperty, value);
                OnABAUFChg();
            }
        }
        private static void OnABAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHVK05;
            if (src != null)
            {
                src.ABAUF = (string)e.NewValue;
            }
        }
        public virtual void OnABAUFChg()
        {
            if(ABAUF=="")
            {
                EXORD = "";
                return;
            }
            EXORD = ABAUF.Substring(1, 11);
        }
        #endregion 
        #region ==EXORD===========
        public static readonly DependencyProperty EXORDProperty = DependencyProperty.Register("EXORD",
            typeof(string), typeof(SJHVK05),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXORDChanged));
        public string EXORD
        {
            get
            {
                return (string)GetValue(EXORDProperty);
            }
            set
            {
                SetValue(EXORDProperty, value);
                OnEXORDChg();
            }
        }
        private static void OnEXORDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHVK05;
            if (src != null)
            {
                src.EXORD = (string)e.NewValue;
            }
        }
        public virtual void OnEXORDChg()
        { }
        #endregion 
        #region ==WFART===========
        public static readonly DependencyProperty WFARTProperty = DependencyProperty.Register("WFART",
            typeof(string), typeof(SJHVK05),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWFARTChanged));
        public string WFART
        {
            get
            {
                return (string)GetValue(WFARTProperty);
            }
            set
            {
                SetValue(WFARTProperty, value);

            }
        }
        private static void OnWFARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHVK05;
            if (src != null)
            {
                src.WFART = (string)e.NewValue;
            }
        }

        #endregion
        VVRPOS VRP;
        VVKKOPF VKK;
        VVRKOPF VRK;
        VVKPOS VKP;
        VWDECK WD;
        VWGBST WG;
        VTXTIND TXT;
        UIWS22SJH UBJ;
        VICC_FOLDERS fl;
        ITXABPLANData TAB = new ITXABPLANData();

        public SJHVK05()
        {

            InitializeComponent();
            this.Name = "销售管理";
            SetUp();
        }
        private void SetUp()
        {
            List<string> deftxt = new List<string>();
            deftxt = new List<string>();
            deftxt.Add("00please type your remark after remove this:");
            cVK02.PRTYPE = "TSTVK04";
            cVK02.IsCheckKD = true;
            //cVKK.LFD = "000000";
        }
        string exauf = "";
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        { 
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            SetUpUserRight();
            IsLoaded = true;
        } 
        public override void OnChanged(bool value)
        {
            if (!value)
                return;
            EXORD = "";
            EXORD = exauf.Substring(0, 11);
            INFO = "初始化已经完成,可以进行评估";
            base.OnChanged(value);
        }
        private void StartABCommand(object sender, RoutedEventArgs e)
        {
            if (WFART == "")
                return;
            string wfart = WFART  + Helper.Left(Helper.ConvertGB2312To8859P1("".PadRight(20)), 20);
            string ABAUF = "H" + cVKP.KEY.Substring(1, 10);
            exauf = ABAUF.Substring(0, 11)
                    + wfart
                     + Helper.Left("".PadRight(8), 8)
                     + Helper.Left("", 20)
                     + Helper.Left("", 20);

            try
            {
                IsChanged = false;
                Dictionary<string, object> PM = cRZ.PM;
                PM["RUNID"] = this.RUNID;
                PM["KRS"] = exauf.Substring(0, 2);
                PM["AUFART"] = "VA";
                PM["ORDERNUM"] = exauf.Substring(0, 11);
                PM["LFD"] = exauf.Substring(2, 6);
                PM["RELOADABK"] = false;
                ABAUF = cRZ.TMV.VABK.NEWABK(PM);

                PM["ORDERNUM"] = ABAUF.Substring(0, 8) + exauf.Substring(8, 3) + "VA ";
                PM["COLOR"] = "VA ";
                PM["AUFTYP"] = "V";
                PM["ARTSTAGE"] = "Z";
                //PM["VKAUF"] = pm["ORDERNUM"];
                PM["VKPAUF"] = ABAUF.Substring(0, 8) + exauf.Substring(8, 3);
                PM["VKPQTY"] = cVKP.TMV.VKPMENGE1 * 0.01;
                PM["VKAUF"] = cVKP.AUF.Substring(1,11)+"00K";//
                PM["QTYEND"] = cVKP.TMV.VKPMENGE1*0.01;
                PM["UNDES"] = "HZ000001";
                PM["DELDATE"] = cRZ.TMV.date;
                PM["STARTDATE"] = cRZ.TMV.date;
              
                PM["P6"] = exauf.Substring(129, 10);//PARTIE
                bool IsExists = cRZ.TMV.VPR.NEWPLAN(PM);
                MessageInfo msg = new MessageInfo();
                msg.Sender = this.RUNID;
                msg.MSGTYPE = "RZABPNEW";
                msg.ACTION = "RZABPNEW";
                msg.AUFTYP = "V";
                msg.WAKZ = exauf.Substring(11, 1);
                msg.ART = exauf.Substring(12, 20);
                msg.VAR = exauf.Substring(32, 2).PadRight(3);
                msg.FARBE = exauf.Substring(35, 10);
                msg.BESCHICH = exauf.Substring(45, 10);
                msg.DESS = exauf.Substring(55, 10);
                msg.COLORIT = exauf.Substring(65, 10);
                msg.ORDER = (string)PM["ORDERNUM"];
                msg.Where = app.User;
                VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void StartKACommand(object sender, RoutedEventArgs e)
        {
            try
            {
                cWD.IsSaved = false;
                cWD.IsSaved = true;
                string krs = EXORD.Substring(0, 2);
                string lfd = EXORD.Substring(2, 6);
                string pos = EXORD.Substring(8, 3);
                Dictionary<string, string> list = (Dictionary<string, string>)TAB.GETON(krs, lfd, pos);
                string key = krs + "_" + lfd + "_" + pos;
                list.Add(key, "YES");
                //删除预估结果
                foreach (KeyValuePair<string, string> kp in list)
                {
                    key = kp.Key;
                    krs = key.Substring(0, 2);
                    lfd = key.Substring(3, 6);
                    pos = key.Substring(10, 3);

                    MCO.PM["KRS"] = key.Substring(0, 2);
                    MCO.PM["LFD"] = key.Substring(3, 6);
                    MCO.PM["POS"] = key.Substring(10, 3);
                    MCO.ResetMCO(MCO.PM);//删除本身
                    VMUI.ResetMAT(MCO.PM);//删除原料
                }
                //插入需求关系
                foreach (KeyValuePair<string, string> kp in list)
                {
                    key = kp.Key;
                    krs = key.Substring(0, 2);
                    lfd = key.Substring(3, 6);
                    pos = key.Substring(10, 3);

                    MCO.PM["KRS"] = key.Substring(0, 2);
                    MCO.PM["LFD"] = key.Substring(3, 6);
                    MCO.PM["POS"] = key.Substring(10, 3);
                    MCO.PrepareProdCost(MCO.PM);//插入本身
                    VMUI.PrepareMatCost(MCO.PM);//插入原料
                }
                //计算经纱成本
                foreach (KeyValuePair<string, string> kp in list)
                {
                    key = kp.Key;
                    krs = key.Substring(0, 2);
                    lfd = key.Substring(3, 6);
                    pos = key.Substring(10, 3);
                    if (krs.Substring(0,1) != "S") continue;
                    MCO.PM["KRS"] = key.Substring(0, 2);
                    MCO.PM["LFD"] = key.Substring(3, 6);
                    MCO.PM["POS"] = key.Substring(10, 3);
                    MCO.UpdateProdCost(MCO.PM);//经轴加工费
                    VMUI.UpdateMatCost(MCO.PM);//经纱成本
                    MCO.CalculateCost(MCO.PM);//经轴总费用，并更新到织造费用
                }
                //计算纬纱
                foreach (KeyValuePair<string, string> kp in list)
                {
                    key = kp.Key;
                    krs = key.Substring(0, 2);
                    lfd = key.Substring(3, 6);
                    pos = key.Substring(10, 3);
                    if (krs.Substring(0, 1) != "W") continue;
                    MCO.PM["KRS"] = key.Substring(0, 2);
                    MCO.PM["LFD"] = key.Substring(3, 6);
                    MCO.PM["POS"] = key.Substring(10, 3);
                    MCO.UpdateProdCost(MCO.PM);//织造加工费
                    VMUI.UpdateMatCost(MCO.PM);//纬纱成本
                    MCO.CalculateCost(MCO.PM);//坯布总费用，并更新到成品
                }
                foreach (KeyValuePair<string, string> kp in list)
                {
                    key = kp.Key;
                    krs = key.Substring(0, 2);
                    lfd = key.Substring(3, 6);
                    pos = key.Substring(10, 3);
                    if (krs.Substring(0, 1) != "H") continue;
                    MCO.PM["KRS"] = key.Substring(0, 2);
                    MCO.PM["LFD"] = key.Substring(3, 6);
                    MCO.PM["POS"] = key.Substring(10, 3);
                    MCO.UpdateProdCost(MCO.PM);//整理加工费
                    VMUI.UpdateMatCost(MCO.PM);//坯布成本
                    MCO.CalculateCost(MCO.PM);//整理总费用
                }
                key = EXORD;
                EXORD = "";
                EXORD = key;

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
    }
}
